if lib and not lib.GetLastCandle then


function lib:GetLastCandle(source, index)		-- 0 - last currently forming candle
	if not index or index < 0 then index = 0 end
	local t = type(source)
	if t == "string" then									-- chart tag
		local nc = getNumCandles(source)
		if nc and nc > index then
			local t, n, l = getCandlesByIndex(source, 0, nc - 1 - index, 1)
			if t and n > 0 then
				local candle = t[n - 1]
				if candle then
					candle.instrument = self:GetChartInstrument(source)
				end
				return candle
			end
		end
	elseif t == "table" and source.SetEmptyCallback then	-- data source
		local nc = source:Size()
		if nc and nc > index then
			local n = nc - index
			return {
				open = source:O(n),
				close = source:C(n),
				high = source:H(n),
				low = source:L(n),
				volume = source:V(n),
				datetime = source:T(n),
				instrument = source.instrument
			}
		end
	end
end


function lib:GetCandleRange(source, index, count, body_only)
	local low, high, is_long
	local candle
	local mi, ma
	for i = 0, count - 1 do
		candle = lib:GetLastCandle(source, index + i)
		if candle then
			if body_only then
				mi = math.min(candle.open, candle.close)
				ma = math.max(candle.open, candle.close)
			else
				mi = candle.low
				ma = candle.high
			end
			if not low or low > mi then
				low = mi
				is_long = true
			end
			if not high or high < ma then
				high = ma
				is_long = false
			end
		else
			return
		end
	end
	return low, high, is_long
end


end -- of file
